<!--
 * @Author: wang 1062637288@qq.com
 * @Date: 2022-12-16 20:32:49
 * @LastEditors: wang 1062637288@qq.com
 * @LastEditTime: 2022-12-17 10:54:31
 * @FilePath: \app\src\pages\auth\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <view>
        <button type="primary" plain open-type="getUserInfo" @getuserinfo="handleGetUserInfo">获取授权</button>
    </view>
</template>

<script>
import { request } from '../../request/index.js';
import { login } from '../../utils/async';
export default {
    data() {
        return {};
    },
    methods: {
        // 获取用户信息
        async handleGetUserInfo(e) {
            try {
                // 1 获取用户信息
                const { encryptedData, rawData, iv, signature } = e.detail;
                // 2 获取小程序登录成功后的code
                const { code } = await login();
                const loginParams = {
                    encryptedData,
                    rawData,
                    iv,
                    signature,
                    code
                };
                //  3 发送请求 获取用户的token
                const { token } = await request({
                    url: '/users/wxlogin',
                    data: loginParams,
                    method: 'post'
                });

                // 4 把token存入缓存中 同时跳转回上一个页面
                uni.setStorageSync('token', token);
                uni.navigateBack({
                    delta: 1
                });
            } catch (error) {
                console.log(error);
            }
        }
    }
};
</script>
<style>
@import './index.css';
</style>
